//index.js
let app = getApp();
Page({
    data: {
        imgUrls: [
            {
                url: '/images/banner/banner1.jpg'
            }, {
                url: '/images/banner/banner2.jpg'
            }, {
                url: '/images/banner/banner3.jpg'
            }


        ],
        currentTab: 0, //预设当前项的值
        lessions: [
            {
                name: '视频',
                index: 3,
            },
            {
                name: '图文',
                index: 1,
            },
            {
                name: '音频',
                index: 2,
            },
        ],
        scrollY: 0
    },
    // 跳转文章页面
    goArticle(e) {
        let number = e.currentTarget.dataset.index;
        wx.navigateTo({
            url:'/pages/article-detail/index?courseNumber='+number
        })
    },
    // 点击标题切换当前页时改变样式
    swichNav: function (e, flag) {
        this.setData({
            currentTab: e.currentTarget.dataset.index
        });
    },
    //判断当前滚动超过一屏时，设置tab标题滚动条。

    onLoad(options) {
        console.log(options);
        app.globalData.share = options;
        if (options.url) {
            console.log(123);
            wx.navigateTo({
                url: getUrl(options)
            });
        }
   // 获取url
        function getUrl(options) {
            var str = options.url + '?';
            for (var key in options) {
                str += key + '=' + options[key] + '&';
            }
            console.log(str);
            return str;
        }

        let {lessions} = this.data;

        lessions.forEach(item => {
            item.data = [];
            item.page = {
                isLoading: false,
                isShowLoading: true,
                isComplete: false,
                total: null,
                currentIndex: 1,
                size: 20,
            }
        })
        this.setData({lessions});
        this.scrollLower();
        // 加载banner

        wx.$ajax.getBannerList({
            "start": 1,
            "size": 10
        }, {isToken: true})
            .then(({data: {data, msg, status}, header, statusCode, errMsg}) => {
                if (status === '0') {
                    this.setData({
                        imgUrls: (data.bannerList || [])
                    })
                } else {
                    console.log('请求失败');
                }
            })
        wx.$ajax.getSystemOptions()
            .then(({data: {data, msg, status}, header, statusCode, errMsg}) => {
                if (status === '0') {
                    this.setData({
                        tipStatus: data.systemSetting.isCharge
                    })
                }
            })

    },
    // 滚动到最低处
    scrollLower() {
        let {lessions, currentTab} = this.data;
        let index = currentTab;
        let lession = lessions[index], page = lession.page;
        if (page.isLoading || page.isComplete) {
            console.log(`${page.isLoading ? '正在加载...' : '已加载完成'}`);
        } else {
            wx.$ajax.getLessionList({
                "start": page.currentIndex,
                "size": page.size,
                "courseType": wx.typeFilter[index]
            }, {isToken: true})
                .then(({data: {data, msg, status}, header, statusCode, errMsg}) => {
                    if (status === '0') {
                        lession.data = lession.data.concat(data.courseList || []);
                        page.currentIndex++;
                    }
                    page.isLoading = false;
                    page.total = data.total;
                    if (page.total <= lession.data.length) {
                        page.isComplete = true;
                        page.isShowLoading = false;
                    }
                    this.setData({lessions})
                })

        }
    },
})
